<?php
/**
 * Created by PhpStorm.
 * User: 刘广
 * Date: 2018/5/24
 * Time: 15:41
 */

namespace app\admin\controller\love;


use app\admin\controller\Base;
use think\Db;

class Activity extends Base
{
    /**
     * 获取列表（GET）
     * URL：admin/love.activity/index
     * @return [type] [description]
     */
    public function index()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $res = Db::name('dd_activity_gk')
                ->page($page, $limit)->select();
            foreach ($res as $index => $re) {
                // $res[$index]['status2'] = $status[$re['status']];
                if($res[$index]['update_time'] == null){
                    $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                }else{
                    $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                    $res[$index]['update_time'] = date('Y-m-d H:i:s', $re['update_time']);
                }

            }
            return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);

        }else{
            $count =  Db::name('dd_activity_gk')->count();
            $this->assign('count',$count);
            return $this->fetch();
        }

    }
    /**
     * 查看爱心司机详情（GET）
     * URL：admin/love.activity/look
     */
    public function look()
    {
        $id = input('id');
        if (!$id) {
            $this->error('id 异常');
        }
        $res = Db::name('dd_activity_gk')->find($id);
        $this->assign('item', $res);
        return $this->fetch();

    }
    /**
     * 条件查询（GET）
     * URL：admin/love.activity/cases_search_index
     */
    public function cases_search_index()
    {
        $log = Db::name('dd_activity_gk');

        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        // 判断姓名是否为空;



            $user = '%'.trim(input('tel')).'%';
            $res= $log
                ->page($page, $limit)
                ->where("nickname|realname|tel",'like',$user)
                ->select();
            foreach ($res as $index => $re) {
                // $res[$index]['status2'] = $status[$re['status']];

                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
            }
            $total = $log->page($page, $limit)
                ->where("nickname|realname|tel",'like',$user)
                ->count();
            return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);

        }
    /**
     * 设置用户状态冻结或启用（GET）
     * URL：admin/love.activity/set_status
     */

    public function set_status()
    {
        $data = input('');
        // dump($data);die();
        $ids = $data['ids'];
        $status = $data['status'];

        if (!$ids) {
            $this->error('异常');
        }

        Db::name('dd_activity_gk')->where('uid','in',$ids)->update(['status' => $status]);
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        $res = Db::name('dd_activity_gk')
            ->page($page, $limit)->select();

        foreach ($res as $index => $re) {
            // $res[$index]['status2'] = $status[$re['status']];
            if($res[$index]['update_time'] == null){
                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
            }else{
                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                $res[$index]['update_time'] = date('Y-m-d H:i:s', $re['update_time']);
            }

        }
        return json(['code' => 1, 'msg' => '操作成功', 'count' => count($res), 'data' => $res]);

    }
    /**
     * 设置审核状态（GET）
     * URL：admin/love.activity/look
     */

    public function set_status2()
    {
        $data = input('');
        // dump($data);die();
        $ids = $data['ids'];
        $status = $data['status'];

        if (!$ids) {
            $this->error('异常');
        }

        Db::name('dd_activity_gk')->where('uid','in',$ids)->update(['auth_status' => $status]);
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        $res = Db::name('dd_activity_gk')
            ->where('auth_status','neq',0)
            ->page($page, $limit)
            ->select();

        foreach ($res as $index => $re) {
            // $res[$index]['status2'] = $status[$re['status']];
            $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
        }
        return json(['code' => 1, 'msg' => '操作成功', 'count' => count($res), 'data' => $res]);

    }
}